package com.student.tools;

import java.util.ArrayList;
import java.util.List;

import com.student.bean.Student;
import com.student.dao.Dao;

public class Pagination {

	private List<Student> stuList = null;
	private int recordCount = 0;
	private int pageSize = 0;
	private int maxPage = 0;
	
	public Pagination(String keyword, int pageSize) {
		Dao dao = new Dao();
		this.stuList = dao.getStudent(keyword);
		this.recordCount = stuList.size();
		this.pageSize = pageSize;
		this.maxPage = (recordCount%pageSize == 0)?(recordCount/pageSize):(recordCount/pageSize+1);
	}
	
	public List<Student> getPage(int page) {
		List<Student> newList = new ArrayList<Student>();
		try{
			for ( int i = (page-1)*pageSize; i <= page*pageSize-1; i++ )
			{
				try{
					if ( i >= recordCount )
					{
						break;
					}
				} catch ( Exception e ) {
					// ignore
				}
				newList.add(stuList.get(i));
			}
		} catch ( Exception e ) {
			e.printStackTrace();
		}
		return newList;
	}
	
	public int getMaxPage() {
		return maxPage;
	}

	public int getRecordCount() {
		return recordCount;
	}
	
}
